<?php  
class ControllerCommonHome extends Controller {
	public function index() {
		$this->document->setTitle($this->config->get('config_title'));

		$this->data['heading_title'] = $this->config->get('config_title');
		
		$this->load->language('common/home');	
		
		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token='.$this->session->data['token'], 'SSL'),
      		'separator' => false
   		);	
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		//languages
		$this->data['text_counter']=$this->language->get('text_counter');
		$this->data['text_online']=$this->language->get('text_online');
		$this->data['text_categories']= $this->language->get('text_categories');
		$this->data['text_system'] = $this->language->get('text_system');
		$this->data['text_module'] = $this->language->get('text_module');
		$this->data['text_statistics'] = $this->language->get('text_statistics');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_system'] = $this->language->get('text_system');
		$this->data['text_front'] = $this->language->get('text_front');
		$this->data['text_setting'] = $this->language->get('text_setting');		
		$this->data['text_users'] = $this->language->get('text_users');
		$this->data['text_language'] = $this->language->get('text_language');
		$this->data['text_contact'] = $this->language->get('text_contact');
		$this->data['text_backup'] = $this->language->get('text_backup');
		$this->data['text_error_log'] = $this->language->get('text_error_log');
		$this->data['text_delete_cache'] = $this->language->get('text_delete_cache');	
		$this->data['text_categories'] = $this->language->get('text_categories');	
		$this->data['text_data'] = $this->language->get('text_data');	
		$this->data['text_layout'] = $this->language->get('text_layout');
		$this->data['text_menu'] = $this->language->get('text_menu');
		$this->data['text_news'] = $this->language->get('text_news');	
		
		// Links	
		$this->data['office'] = HTTP_CATALOG;	
		$this->data['module_counter'] = $this->url->link('module/counter','token='.$this->session->data['token'],'SSL');
		$this->data['online'] = $this->url->link('catalog/online','token='.$this->session->data['token'],'SSL');
		$this->data['module_categories'] = $this->url->link('module/categories','token='.$this->session->data['token'],'SSL');
		$this->data['setting'] = $this->url->link('setting/setting','token='.$this->session->data['token'],'SSL');
		$this->data['module'] = $this->url->link('extension/module','token='.$this->session->data['token'],'SSL');
		$this->data['user'] = $this->url->link('user/user','token='.$this->session->data['token'],'SSL');
		$this->data['contact'] = $this->url->link('sale/contact','token='.$this->session->data['token'],'SSL');
		$this->data['news'] = $this->url->link('catalog/news','token='.$this->session->data['token'],'SSL');
		$this->data['language'] = $this->url->link('localisation/language','token='.$this->session->data['token'],'SSL');
		$this->data['backup'] = $this->url->link('tool/backup','token='.$this->session->data['token'],'SSL');
		$this->data['error_log'] = $this->url->link('tool/error_log&','token='.$this->session->data['token'],'SSL');
		$this->data['cache'] = $this->url->link('tool/cache','token='.$this->session->data['token'],'SSL');
		$this->data['categories'] = $this->url->link('catalog/category','token='.$this->session->data['token'],'SSL');
		$this->data['menu'] = $this->url->link('catalog/menu','token='.$this->session->data['token'],'SSL');
		$this->data['layout'] = $this->url->link('design/layout','token='.$this->session->data['token'],'SSL');
		
		
		// Module Installed
		
		$this->load->model('setting/extension');

		$extensions = $this->model_setting_extension->getInstalled('module');
		
		foreach ($extensions as $key => $value) {
			if (!file_exists(DIR_APPLICATION . 'controller/module/' . $value . '.php')) {
				$this->model_setting_extension->uninstall('module', $value);
				
				unset($extensions[$key]);
			}
		}
		
		$this->data['extensions'] = array();
						
		$files = glob(DIR_APPLICATION . 'controller/module/*.php');
		
		if ($files) {
			foreach ($files as $file) {
				$extension = basename($file, '.php');
				
				$this->load->language('module/' . $extension);			
				
				if (in_array($extension, $extensions)) {
					$this->data['extensions'][] = array(
						'name'   => $this->language->get('heading_title'),
						'href' => $this->url->link('module/' . $extension . '', 'token=' . $this->session->data['token'], 'SSL')
					);
				}							
				
			}
		}
		
		
		
		
		$this->template = 'template/common/home.tpl';
		
		
		$this->children = array(
			'common/footer',
			'common/header'
		);	
		
										
		$this->response->setOutput($this->render());
	}
	
	public function login() { 
		if (!$this->user->isLogged()) {
			return $this->forward('common/login');
		}
		
		if (isset($this->request->get['route'])) {
			$route = '';
						
			$part = explode('/', $this->request->get['route']);
			
			if (isset($part[0])) {
				$route .= $part[0];
			}
			
			if (isset($part[1])) {
				$route .= '/' . $part[1];
			}
			
			$ignore = array(
				'common/login',
				'common/logout',
				'error/not_found',
				'error/permission'
			);
			
			$config_ignore = array();
			
			if ($this->config->get('config_token_ignore')) {
				$config_ignore = unserialize($this->config->get('config_token_ignore'));
			}
				
			$ignore = array_merge($ignore, $config_ignore);
						
			if (!in_array($route, $ignore)) {
				if (!isset($this->request->get['token']) || !isset($this->session->data['token']) || ($this->request->get['token'] != $this->session->data['token'])) {
					return $this->forward('common/login');
				}
			}
		} else {
			if (!isset($this->request->get['token']) || !isset($this->session->data['token']) || ($this->request->get['token'] != $this->session->data['token'])) {
				return $this->forward('common/login');
			}
		}
	}
}
?>